﻿using CinemaMaestro.DataLayer.Contracts;
using CinemaMaestro.DataLayer.Contracts.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CinemaMaestro.DataLayer.Entity
{
    public class UnitOfWork : IUnitOfWork
    {

        private CinemaContext _ctx;

        public UnitOfWork()
        {
            _ctx = new CinemaContext();
        }

        private IRepository<Language> _languageRepository;
        public IRepository<Language> LanguageRepository
        {
            get
            {
                if (_languageRepository == null)
                {
                    _languageRepository = new LanguageRepository(_ctx);
                }

                return _languageRepository;
            }
        }

        private IRepository<Movie> _movieRepository;
        public IRepository<Movie> MovieRepository
        {
            get
            {
                if (_movieRepository == null)
                {
                    _movieRepository = new MovieRepository(_ctx);
                }

                return _movieRepository;
            }
        }

        private IRepository<Actor> _actorRepository;
        public IRepository<Actor> ActorRepository
        {
            get
            {
                if (_actorRepository == null)
                {
                    _actorRepository = new ActorRepository(_ctx);
                }

                return _actorRepository;
            }
        }

        private IRepository<Category> _categoryRepository;
        public IRepository<Category> CategoryRepository
        {
            get
            {
                if (_categoryRepository == null)
                {
                    _categoryRepository = new CategoryRepository(_ctx);
                }

                return _categoryRepository;
            }
        }

        private IRepository<Director> _directorRepository;
        public IRepository<Director> DirectorRepository
        {
            get
            {
                if (_directorRepository == null)
                {
                    _directorRepository = new DirectorRepository(_ctx);
                }

                return _directorRepository;
            }
        }

        private IRepository<Cinema> _cinemaRepository;
        public IRepository<Cinema> CinemaRepository
        {
            get
            {
                if (_cinemaRepository == null)
                {
                    _cinemaRepository = new CinemaRepository(_ctx);
                }

                return _cinemaRepository;
            }
        }

        private IRepository<Room> _roomRepository;
        public IRepository<Room> RoomRepository
        {
            get
            {
                if (_roomRepository == null)
                {
                    _roomRepository = new RoomRepository(_ctx);
                }

                return _roomRepository;
            }
        }

        private IRepository<Screening> _screeningRepository;
        public IRepository<Screening> ScreeningRepository
        {
            get
            {
                if (_screeningRepository == null)
                {
                    _screeningRepository = new ScreeningRepository(_ctx);
                }

                return _screeningRepository;
            }
        }

        private IRepository<Reservation> _reservationRepository;
        public IRepository<Reservation> ReservationRepository
        {
            get
            {
                if (_reservationRepository == null)
                {
                    _reservationRepository = new ReservationRepository(_ctx);
                }

                return _reservationRepository;
            }
        }

        private IRepository<Sale> _saleRepository;
        public IRepository<Sale> SaleRepository
        {
            get
            {
                if (_saleRepository == null)
                {
                    _saleRepository = new SaleRepository(_ctx);
                }

                return _saleRepository;
            }
        }

        public void SaveChanges()
        {
            _ctx.SaveChanges();
        }

        public IDbTransaction BeginTransaction()
        {
            return new DbTransaction(_ctx);
        }
    }
}
